package ma.ensao.evalmetrics.util.javaProjects;

import japa.parser.JavaParser;
import japa.parser.ParseException;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.expr.MethodCallExpr;
import japa.parser.ast.visitor.VoidVisitorAdapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ma.ensao.evalmetrics.controller.metrics.CLOCManager;
/*
*        Calcule RFC 
*/
public class RFCMetric {
	private File sourceFile;
	private int rfc= 0;
	private int nbreMethods=0;
    public RFCMetric(File sourceFile) throws  IOException {
        // creates an input stream for the file to be parsed
        FileInputStream in = new FileInputStream(sourceFile);
        CompilationUnit cu = null;
        try {
            // parse the file
            cu = JavaParser.parse(in);
        } catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
            in.close();
        }
        // visit and print the methods names
        MethodVisitor MV =new MethodVisitor();
        MV.visit(cu, null);
        cu.accept(new VoidVisitorAdapter<Void>(){
            @Override
            public void visit(final MethodCallExpr n, final Void arg){
            	System.out.println("1"+n);
            	super.visit(n, arg);
            	nbreMethods++;
            }
        }, null);
        setRfc(getNbreMethods()+MV.getNbreMethodsCalls());
        System.out.println(getNbreMethods()+"+"+MV.getNbreMethodsCalls()+"="+rfc);
        
        
        
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, ParseException {
		//test a = new test("C:/Users/Hosni/Documents/test/AdminManager.java");

    	File f = new File("C:/Users/sam/SVN/metricsTest/src/test/metric/fol1/A.java");
    	RFCMetric a=new RFCMetric(f);

		 
	}

    public int getRfc() {
		return rfc;
	}

	public void setRfc(int rfc) {
		this.rfc = rfc;
	}

	public int getNbreMethods() {
		return nbreMethods;
	}

	public void setNbreMethods(int nbreMethods) {
		this.nbreMethods = nbreMethods;
	}

	/**
     * Simple visitor implementation for visiting MethodDeclaration nodes. 
     */
    private static class MethodVisitor extends VoidVisitorAdapter {
    	private int rfc= 0;
    	private int nbreMethodsCalls=0;
		@Override
        public void visit(MethodDeclaration n, Object arg) {
            // here you can access the attributes of the method.
            // this method will be called for all methods in this 
            // CompilationUnit, including inner class methods
           System.out.println("2"+n.getName());
          nbreMethodsCalls++;
            //visit(n.getBody(), arg);
        }
		public int getNbreMethodsCalls() {
			return nbreMethodsCalls;
		}
		public void setNbreMethodsCalls(int nbreMethodsCalls) {
			this.nbreMethodsCalls = nbreMethodsCalls;
		}
    }
}